package com.lap.gateway.tools;

import com.lap.framework.dto.result.Result;
import com.lap.framework.tools.JsonUtil;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

public final class MonoHolder {

  private MonoHolder() {}

  public static Mono<Void> onError(ServerHttpResponse response, Result<?> result) {
    response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
    DataBuffer buffer = response.bufferFactory().wrap(JsonUtil.toBytes(result));
    return response.writeWith(Mono.just(buffer));
  }
}
